<?php namespace app\service\controller;
use think\Controller;
use think\Db;

/**
 * 服务层
 */
class User extends Controller{
	
	private $modelObj;				//模型句柄
	public function __construct(){
		parent::__construct();
		if(empty($this -> modelObj)) $this -> modelObj = new \app\model\model\User;
	}
	
	/**
	 * 获取指定用户角色
	 */
	public function getUserGroup($userId){
		$where[] = ['uid', 'eq', $userId];
		$data = Db::table("gd_auth_group_access") -> where($where) -> find();
		return !empty($data) ? $data : [];
	}
	
	public function getUserLevelInfo($userId){
		$userWhere[] = ['id', 'eq', $userId];
		$data = Db::table("gd_user") -> where($userWhere) -> find();
		$level = [];
		if( !empty($data) ){
			$data['level'] = [];
			$time = time();
			if( $data['level_config_id'] > 0 && strtotime($data['level_start_date']) < $time && strtotime($data['level_end_date']) > $time ){
				$levelWhere[] = ['id', 'eq', $data['level_config_id']];
				$data['level'] = Db::table("gd_level_config") -> where($levelWhere) -> find();
				$level = $data['level'];
			}else{
				$authGroupAccessWhere[] = ['uid', 'eq', $userId];
				$authGroupAccess = Db::table("gd_auth_group_access") -> where($authGroupAccessWhere) -> find();
				if( !empty($authGroupAccess) ){
					$authGroupWhere[] = ['id', 'eq', $authGroupAccess['group_id']];
					$authGroup = Db::table("gd_auth_group") -> where($authGroupWhere) -> find();
					if( $authGroup['level_config_id'] ){
						$levelWhere[] = ['id', 'eq', $authGroup['level_config_id']];
						$data['level'] = Db::table("gd_level_config") -> where($levelWhere) -> find();
						$level = $data['level'];
					}
				}
			}
		}
		return $level;
	}
	
	/**
	 * 新增指定数据
	 */
//	public function addData($in){
//		if(empty($in)){
//			$out_arr['code'] = '40004';
//		}else{
//			
//			$where['username'] = isset($in['username']) ? $in['username'] : '';
//			$where['agency_id'] = isset($in['agency_id']) ? $in['agency_id'] : '';
////			$whereOr['nickname'] = isset($in['nickname']) ? $in['nickname'] : '';
//			$oldData = $this -> modelObj -> getSingleInfo($where);
//			if( !empty($oldData) ) return ['code'=>'-1', 'msg'=>'用户名已存在'];
////			$oldData = $this -> modelObj -> getSingleInfo($whereOr);
////			if( !empty($oldData) ) return ['code'=>'-1', '用户昵称已存在'];
//			
//			$salt = getRandStr();
//
//			$newData['agency_id'] = isset($in['agency_id']) ? $in['agency_id'] : '';
//			$newData['username'] = isset($in['username']) ? $in['username'] : '';
//			$newData['nickname'] = isset($in['nickname']) ? $in['nickname'] : '';
//			$newData['password'] = isset($in['password']) ? md5( md5( trim($in['password']) ) . $salt ) : '';
//			$newData['mobilephone'] = isset($in['mobilephone']) ? $in['mobilephone'] : '';
//			$newData['card_id'] = isset($in['card_id']) ? $in['card_id'] : '';
//			$newData['med'] = isset($in['med']) ? $in['med'] : '';
//			$newData['report_type'] = isset($in['report_type']) ? $in['report_type'] : '';
//			$newData['old_admin_id'] = isset($in['old_admin_id']) ? $in['old_admin_id'] : '';
//			$newData['old_admin_name'] = isset($in['old_admin_name']) ? $in['old_admin_name'] : '';
//			$newData['create_user_id'] = isset($in['create_user_id']) ? $in['create_user_id'] : '0';
//			$newData['create_nickname'] = isset($in['create_nickname']) ? $in['create_nickname'] : '';
//			$newData['court_ids'] = isset($in['court_ids']) ? implode(',', $in['court_ids']) : '';
//			$newData['salt'] = $salt;
//			$newData['createtime'] = time();
//			
//			$adminReportTypeModelObj = new \app\model\model\AdminReportType;
//			$adminCourtModelObj = new \app\model\model\AdminCourt;
//			
//			Db::startTrans();
//			$re = $this -> modelObj -> addData($newData);
//			if(empty($re)){
//				Db::rollback();
//				$outArr['code'] = '99997';
//				$outArr['info'] = $re;
//			}
//			
//			$reportType = explode(',', $newData['report_type']);
//			$newReportType = [];
//			foreach ($reportType as $k => $v) {
//				$newReportType[$k]['admin_id'] = $re;
//				$newReportType[$k]['report_type'] = $v;
//			}
//			$addRes = $adminReportTypeModelObj -> addAll($newReportType);
//			if( !$addRes ){
//				Db::rollback();
//				$outArr['code'] = '99997';
//				$outArr['info'] = '新增账号原案件类型失败';
//				return $outArr;
//			}
//
//			$court = explode(',', $newData['court_ids']);
//			$newCourt = [];
//			foreach ($court as $k => $v) {
//				$newCourt[$k]['admin_id'] = $re;
//				$newCourt[$k]['court_id'] = $v;
//			}
//			$addRes = $adminCourtModelObj -> addAll($newCourt);
//			if( !$addRes ){
//				Db::rollback();
//				$outArr['code'] = '99997';
//				$outArr['info'] = '新增账号法院失败';
//				return $outArr;
//			}
//			
//			if( isset($in['group_id']) && !empty($in['group_id']) ){
//				$group_id = explode(',', $in['group_id']);
//				$newGroup = [];
//				foreach ($group_id as $key => $value) {
//					$newGroup[$key]['uid'] = $re;
//					$newGroup[$key]['group_id'] = $value;
//				}
//				
//				$authGroupAccessModelObj = new \app\model\model\AuthGroupAccess;
//				$res = $authGroupAccessModelObj -> addAll($newGroup);
//				if(empty($re)){
//					Db::rollback();
//					$outArr['code'] = '99997';
//					$outArr['info'] = $re;
//				}
//			}
//			
//			if( isset($in['dep_id']) && !empty($in['dep_id']) ){
//				$dep_id = explode(',', $in['dep_id']);
//				$newDep = [];
//				foreach ($dep_id as $key => $value) {
//					$newDep[$key]['admin_id'] = $re;
//					$newDep[$key]['department_id'] = $value;
//				}
//				
//				$departmentAdminModelObj = new \app\model\model\DepartmentAdmin;
//				$res = $departmentAdminModelObj -> addAll($newDep);
//				if(empty($re)){
//					Db::rollback();
//					$outArr['code'] = '99997';
//					$outArr['info'] = $re;
//				}
//			}
//			
//			Db::commit();
//			$outArr['code'] = '1';
//		}
//		return $outArr;
//	}
	
	/**
	 * 编辑指定数据
	 */
	public function editData($in){
		if(empty($in)){
			$out_arr['code'] = '40004';
		}else{

			$where['id'] = isset($in['userId']) ? trim($in['userId']) : '';				//
			$where = filterArr($where, array_keys($where), []);
			if( empty($where) ) return ['code'=>'40004'];
			
			$oldData = $this -> modelObj -> getSingleInfo($where);
			if( empty($oldData) ) return ['code'=>'99999'];
			
			$save['status'] = isset($in['status']) ? $in['status'] : $oldData['status'];
			$save['password'] = isset($in['password']) ? md5( md5( trim($in['password']) ) ) : $oldData['password'];
			$save['logintime'] = isset($in['logintime']) ? $in['logintime'] : $oldData['logintime'];
			$save['loginip'] = isset($in['loginip']) ? $in['loginip'] : $oldData['loginip'];
			$save['login_num'] = isset($in['login_num']) ? $in['login_num'] : $oldData['login_num'];
			
			Db::startTrans();
			
			$save = filterArr($save, array_keys($save), $oldData);
			if( !empty($save) ){
				$re = $this -> modelObj -> editData($where, $save);
				if( !$re ){
					Db::rollback();
					$outArr['code'] = '99997';
					$outArr['info'] = '编辑账号失败';
					return $outArr;
				}
				
				
			}

			if( isset($in['group_id']) && !empty($in['group_id']) ){
				$authGroupAccessModelObj = new \app\model\model\AuthGroupAccess;
				$oldGroup = $authGroupAccessModelObj -> getSingleInfo(['uid'=>$in['adminId']], ['group_id']);
				if(!empty($oldGroup)){
					$delRes = $authGroupAccessModelObj -> delData(['uid'=>$in['adminId']]);
					if( !$delRes ){
						Db::rollback();
						$outArr['code'] = '99997';
						$outArr['info'] = '删除账号原角色失败';
						return $outArr;
					}
				}
				$group_id = explode(',', $in['group_id']);
				$newGroup = [];
				foreach ($group_id as $key => $value) {
					$newGroup[$key]['uid'] = $in['adminId'];
					$newGroup[$key]['group_id'] = $value;
				}
				$res = $authGroupAccessModelObj -> addAll($newGroup);
				if( !$res ){
					Db::rollback();
					$outArr['code'] = '99997';
					$outArr['info'] = '编辑账号角色失败';
					return $outArr;
				}
			}
			
//			if( isset($in['dep_id']) && !empty($in['dep_id']) ){
//				$departmentAdminModelObj = new \app\model\model\DepartmentAdmin;
//				$oldDep = $departmentAdminModelObj -> getSingleInfo(['admin_id'=>$in['adminId']], ['department_id']);
//				if(!empty($oldDep)){
//					$delRes = $departmentAdminModelObj -> delData(['admin_id'=>$in['adminId']]);
//					if( !$delRes ){
//						Db::rollback();
//						$outArr['code'] = '99997';
//						$outArr['info'] = '删除账号原部门失败';
//						return $outArr;
//					}
//				}
//				$dep_id = explode(',', $in['dep_id']);
//				$newDep = [];
//				foreach ($dep_id as $key => $value) {
//					$newDep[$key]['admin_id'] = $in['adminId'];
//					$newDep[$key]['department_id'] = $value;
//				}
//				$res = $departmentAdminModelObj -> addAll($newDep);
//				if( !$res ){
//					Db::rollback();
//					$outArr['code'] = '99997';
//					$outArr['info'] = '编辑账号部门失败';
//					return $outArr;
//				}
//			}
			
			Db::commit();
			$outArr['code'] = '1';
			$outArr['oldData'] = $oldData;
		}
		return $outArr;
	}
	
	/**
	 * 获取多条数据
	 */
//	public function getMuchData($in){
//		$where['med'] = isset($in['med']) ? trim($in['med']) : '';							//
//		$where['agency_id'] = isset($in['agency_id']) ? trim($in['agency_id']) : '';		//
//		$where['status'] = isset($in['status']) ? trim($in['status']) : '';					//
//		$where['is_statistics'] = isset($in['is_statistics']) ? trim($in['is_statistics']) : '';					//
//		
//		$orderBy = isset($in['orderBy']) ? $in['orderBy'] : '';								//排序
//		$groupBy = isset($in['groupBy']) ? $in['groupBy'] : '';								//分组
//		$page = isset($in['page']) ? $in['page'] : '';										//页码
//		$num = isset($in['num']) ? $in['num'] : '';											//数量
//		
//		$whereExp = [];
//		foreach ($in as $key => $value) {
//			if( gettype($key) == 'integer' && in_array($value[1], ['in', 'not in', 'like', 'between', 'eq', 'neq', 'lt', 'lt']) && count($value) == 3 ) $whereExp[] = [$value[0], $value[1], $value[2]];
//		}
//		
//		$field = isset($in['field']) ? $in['field'] : '';									//要获取的字段
//		
//		$where = filterArr($where, array_keys($where), []);
//		
//		$data = $this -> modelObj -> getMuchInfo($where, $field, $orderBy, $groupBy, $page, $num, $whereExp);
//		
//		if(!$data){
//			$outArr['code'] = '99999';
//		}else{
//			$outArr['code'] = '1';
//			$outArr['info'] = $data;
//			
////			if( !empty($page) && !empty($num) ){
//				$outArr['totalNums'] = $this -> modelObj -> getCountNums($where, $groupBy, $whereExp);
////			}
//		}
//		return $outArr;
//	}
	
	/**
	 * 获取指定数据
	 */
	public function getSingleData($in){
		if(empty($in)){
			$out_arr['code'] = '40004';
		}else{

			$where['nickname'] = isset($in['nickname']) ? trim($in['nickname']) : '';				//
			$where['id'] = isset($in['userId']) ? trim($in['userId']) : '';				//
			$where['phone'] = isset($in['phone']) ? trim($in['phone']) : '';				//
			$field = isset($in['field']) ? $in['field'] : '';										//要获取的字段
			
			$where = filterArr($where, array_keys($where), []);
			
			$data = $this -> modelObj -> getSingleInfo($where, $field);
			if(!$data){
				$outArr['code'] = '99999';
			}else{
				$outArr['code'] = '1';
				$outArr['info'] = $data;
			}
		}
		return $outArr;
	}
	
	/**
	 * 获取多条数据
	 */
//	public function getJoinAdminReportTypeMuchData($in){
//		$where['a.agency_id'] = isset($in['a.agency_id']) ? trim($in['a.agency_id']) : '';		//
//		
//		$orderBy = isset($in['orderBy']) ? $in['orderBy'] : '';								//排序
//		$groupBy = isset($in['groupBy']) ? $in['groupBy'] : '';								//分组
//		$page = isset($in['page']) ? $in['page'] : '';										//页码
//		$num = isset($in['num']) ? $in['num'] : '';											//数量
//		
//		$whereExp = [];
//		foreach ($in as $key => $value) {
//			if( gettype($key) == 'integer' && in_array($value[1], ['in', 'not in', 'like', 'between', 'eq', 'neq', 'lt', 'lt']) && count($value) == 3 ) $whereExp[] = [$value[0], $value[1], $value[2]];
//		}
//		
//		$field = isset($in['field']) ? $in['field'] : '';									//要获取的字段
//		
//		$where = filterArr($where, array_keys($where), []);
//		
//		$data = $this -> modelObj -> getJoinAdminReportTypeMuchInfo($where, $field, $orderBy, $groupBy, $page, $num, 'inner', $whereExp);
//		
//		if(!$data){
//			$outArr['code'] = '99999';
//		}else{
//			$outArr['code'] = '1';
//			$outArr['info'] = $data;
//			
//			if( !empty($page) && !empty($num) ){
//				$outArr['totalNums'] = $this -> modelObj -> getJoinAdminReportTypeCount($where, $groupBy, $whereExp);
//			}
//		}
//		return $outArr;
//	}
	
}
